package com.android.cricle;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.widget.ImageView;

public class Circleview extends ImageView implements Runnable {

  private Bitmap mHourBitmap;

  private boolean bInitComplete = false;
  private boolean stopRoter = true;
  float Angel = 0.0f;
  Matrix matx = new Matrix();//对bitmap进行处理

  float maxAngel = 0.0f;

  int screnWidth = 0;

  public Circleview(Context context, int width) {
    super(context);
    this.screnWidth = width;
    init();
    new Thread(this).start();
  }

  public void init() {
    //设置图片
    mHourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.share_lottery_pointer);
    bInitComplete = true;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    matx.reset();
    canvas.drawColor(Color.TRANSPARENT);

    if (!bInitComplete) {
      return;
    }
    Paint localPaint = new Paint();
    //以下两个都是消除锯齿函数
    localPaint.setAntiAlias(true);
    localPaint.setFilterBitmap(true);
    /**
     * 平移
     */
    matx.setTranslate(this.screnWidth / 2 - mHourBitmap.getWidth() / 2,
        DensityUtil.dip2px(getContext(), 300) / 2 - mHourBitmap.getHeight() + DensityUtil.dip2px(
            getContext(), 20));
    /**
     * 以mHourBitmap.getWidth() / 2,mHourBitmap.getHeight() * 4 / 5) 为中心旋转
     */
    matx.preRotate(Angel, mHourBitmap.getWidth() / 2, mHourBitmap.getHeight() * 4 / 5);

    canvas.drawBitmap(mHourBitmap, matx, localPaint);
  }

  public void run() {
    try {
      while (true) {
        if (!stopRoter) {
          if (maxAngel != 0 && Angel >= maxAngel) {
            setStopRoter(true);
            maxAngel = 0.0f;
          } else {
            if (maxAngel - Angel < 360) {
              Angel += 10;
            } else {
              Angel += 15;
            }
            this.postInvalidate();//刷新界面
            Thread.sleep(50);
          }
        }
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  /**
   * 设置停止的位置
   */

  public void setStopPlace(int place) {
    getRoterByPlace(place);
  }

  /**
   * ˳ʱ����ת
   * 1 = 330-30
   * 2 = 30-90
   * 3 = 90-150
   * 4 = 150-210
   * 5 = 210-270
   * 6 = 270-330
   */
  void getRoterByPlace(int place) {
    float roter = getRoteCenter(place)//要达到的中心
    float currentRoter = getCurrentRoter();//当前的中心

    float difRoter = currentRoter - roter;//相差多少
    maxAngel = Angel + 360 * 2 + 360 - difRoter;
  }

  /**
   * 某区域的中心位置
   */
  float getRoteCenter(int place) {
    float roter = 0.0f;
    switch (place) {
      case 1:
        roter = 0;
        break;
      case 2:
        roter = 60 / 2 + 30;
        break;
      case 3:
        roter = 60 / 2 + 90;
        break;
      case 4:
        roter = 60 / 2 + 150;
        break;
      case 5:
        roter = 60 / 2 + 210;
        break;
      case 6:
        roter = 60 / 2 + 270;
        break;
      default:
        break;
    }
    return roter;
  }

  /**
   *获得当前的角度
   */
  float getCurrentRoter() {
    int current = (int) Angel / 360;

    if (0 == current) {
      return Angel;
    }

    float roter = Angel - 360 * current;
    return roter;
  }

  public void setStopRoter(boolean stopRoter) {
    this.stopRoter = stopRoter;
  }

}